// pages/tickets_detail/index.js
import {navTo,navToTab,onInput,onClick,showText,addCardList,total} from '../../utils/reuse'
import{ getShopEdtail } from '../../api/index'
import {formatTime} from '../../utils/util'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import {baseURL} from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    width:wx.getSystemInfoSync().windowWidth,
    all:{},
    cartDetail:false,
    cartTotal:0
  },

  onClickButton(){
    var obj = {
      goods_id:this.data.all.id,
      goods_name:this.data.all.name,
      goods_price:this.data.all.price,
      goods_count:1,
      goods_small_logo:this.data.all.images[0],
      goods_state:true
    }
    var res = addCardList(obj)
    if(res == 1){
      this.setData({
        cartDetail:true
      })
    }
    total()
    var carttotal = wx.getStorageSync('cart').length || 0
    if(carttotal){
      this.setData({
        cartTotal:carttotal,
      })
    }
  },
  onClickImmediately(){
    var obj = {
      goods_id:this.data.all.id,
      goods_name:this.data.all.name,
      goods_price:this.data.all.price,
      goods_count:1,
      goods_small_logo:this.data.all.images[0],
      goods_state:true
    }
    var arr = []
    arr.push(obj)

    wx.setStorageSync('goods_lj', arr)

    wx.navigateTo({
      url: '/pages/tickets_order/index?id=' + 1,
    })

  },
  getList(id){
    var obj = {
      id
    }
    getShopEdtail(obj).then(res=>{
      if(res.sta == 1){
        if(res.detail.content){
          //这个必加
          res.detail.content = res.detail.content.replace(/style="width: [1-9][0-9][0-9]px;/g, `style="width:100%;height:auto;float:left;display:block;`);
        }
        if(res.detail.images){
          var arr1 = []
          res.detail.images = res.detail.images.split(',')
          res.detail.images.forEach(item=>{
            arr1.push(baseURL + item)
          })
          res.detail.images = arr1
        }
        var cartList = wx.getStorageSync('cart') || []
        var detailcart = cartList.find(item=>item.goods_id == res.detail.id)
        if(detailcart){
          this.setData({
            cartDetail:true
          })
        }else{
          this.setData({
            cartDetail:false
          })
        }
        this.setData({
          all:res.detail
        })
      }else{
        showText('数据获取失败！',2)
      }
    })
  },
  onClickIcon(){
    wx.switchTab({
      url: '/pages/card/index',
    })
  },

  nav(e){
    navTo(e)
  },
  click(e){
    onClick(e,this)
  },
  change(e){
    onInput(e,this)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.getList(options.id)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var cartList = wx.getStorageSync('cart') || []
    var detailcart = cartList.find(item=>item.goods_id == this.data.all.id)
    if(detailcart){
      this.setData({
        cartDetail:true
      })
    }else{
      this.setData({
        cartDetail:false
      })
    }
    var carttotal = wx.getStorageSync('cart').length || 0
    if(carttotal){
      this.setData({
        cartTotal:carttotal
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})